import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import ErrorExcption from 'src/error/ErrorExcption';
import { Repository } from 'typeorm';
import { Live } from '../live/entities/live.entity';
import { CreateMovieDto } from './dto/create-movie';
import { Movie } from './entities/movies.entity';

@Injectable()
export class MoviesService {
  constructor(
    @InjectRepository(Movie)
    private movieRepository: Repository<Movie>,
    @InjectRepository(Live)
    private liveRepository: Repository<Live>,
  ) {}

  async create(createMovie: CreateMovieDto) {
    try {
      // const live = await this.liveRepository.findOne({
      //   where: {
      //     id: createMovie.liveid,
      //   },
      // });

      // if (!live) {
      //   throw new ErrorExcption('未找到房间', 200);
      // }

      await this.movieRepository.save(createMovie);
    } catch (error) {
      throw new ErrorExcption(error, 200);
    }
  }

  async remove(id: string) {
    return await this.movieRepository.delete({ id });
  }

  async findAll(id: string) {
    return await this.movieRepository.find({ where: { liveId: id } });
  }
}
